// 解析 base64 字符串
function parseBase64String(base64String) {
    const binaryString = atob(base64String);
    const binaryLength = binaryString.length;
    const bytes = new Uint8Array(binaryLength);
    for (let i = 0; i < binaryLength; i++) {
        bytes[i] = binaryString.charCodeAt(i);
    }
    return bytes;
}

// 转换解析后的数据为 Blob 对象
function convertToBlob(parsedData, options = {}) {
    const { sampleRate = 12000, bitRate = 16, numChannels = 1 } = options;

    // 根据 bitRate 转换解析后的数据为对应的 Int16Array
    let res;
    if (bitRate === 16) {
        res = new Int16Array(parsedData.buffer);
    } else if (bitRate === 8) {
        res = new Int16Array(parsedData.length);
        for (let j = 0; j < parsedData.length; j++) {
            const b = parsedData[j];
            res[j] = (b - 128) << 8;
        }
    } else if (bitRate === 24) {
        res = new Int16Array(parsedData.length / 3);
        for (let i = 0, j = 0; j < parsedData.length;) {
            let n = ((parsedData[j++] | (parsedData[j++] << 8) | (parsedData[j++] << 16)) << 8) >> 8;
            n = n / 16777216;
            res[i++] = n * 0x7FFF;
        }
    }

    // 分离声道
    let resA = res;
    let resB = res;
    if (numChannels === 2) {
        resA = new Int16Array(res.length / 2);
        resB = new Int16Array(res.length / 2);
        for (let i = 0; i < resA.length; i++) {
            resA[i] = res[i * 2];
            resB[i] = res[i * 2 + 1];
        }
    }

    // 进行采样率的调整和截取等操作...

    // 组装 WAV 格式的数据并转换为 Blob 对象
    const dataLength = resA.length * (bitRate / 8) * numChannels;
    const buffer = new ArrayBuffer(44 + dataLength);
    const data = new DataView(buffer);

    var offset = 0;
    var writeString = function (str) {
        for (var i = 0; i < str.length; i++, offset++) {
            data.setUint8(offset, str.charCodeAt(i));
        };
    };
    var write16 = function (v) {
        data.setUint16(offset, v, true);
        offset += 2;
    };
    var write32 = function (v) {
        data.setUint32(offset, v, true);
        offset += 4;
    };

    /* RIFF identifier */
    writeString('RIFF');
    /* RIFF chunk length */
    write32(36 + dataLength);
    /* RIFF type */
    writeString('WAVE');
    /* format chunk identifier */
    writeString('fmt ');
    /* format chunk length */
    write32(16);
    /* sample format (raw) */
    write16(1);
    /* sample rate */
    write32(sampleRate);
    /* bits per sample */
    write16(bitRateNew);
    /* data chunk identifier */
    writeString('data');
    /* data chunk length */
    write32(dataLength);
    // 写入采样数据
    if (bitRateNew == 8) {
        for (var i = 0; i < size; i++, offset++) {
            //16转8
            var val = (resA[i] >> 8) + 128;
            data.setInt8(offset, val, true);

        };
    } else {
        for (var i = 0; i < size; i++, offset += 2) {
            data.setInt16(offset, resA[i], true);

        };
    };
    return new Blob([data.buffer], { type: "audio/wav" });
}

// 将 base64 字符串转换为 Blob 对象的整体函数
function base64ToBlob(base64String) {
    const parsedData = parseBase64String(base64String);
    const options = {
        sampleRate: 16000,
        bitRate: 16,
        numChannels: 1
    }
    return convertToBlob(parsedData, options);
}

// 示例用法
const base64String = "your_base64_string_here";
const blob = base64ToBlob(base64String);
const url = URL.createObjectURL(blob);
console.log(url); // 输出 Blob 对象的 URL
